package com.jlastu.dlxy_androidapp.view.main;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.SelectGoodsListAdapter;
import com.jlastu.dlxy_androidapp.base.BaseFragment;
import com.jlastu.dlxy_androidapp.contract.SelectContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.presenter.SelectGoodsPresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;
import com.jlastu.dlxy_androidapp.view.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class SelectFragment extends BaseFragment implements View.OnClickListener, SelectContract.ISelectView {

    public static final String ARG_GOODS_NAME = "ARG_GOODS_NAME";

    private String goodsName;
    private RecyclerView mSelectRecyclerView;

    private EditText mSelectEdit;

    private SelectGoodsListAdapter mSelectRecyclerAdapter;

    private SelectContract.ISelectPresenter mSelectPresenter;

    private List<Goods> mGoodsList;

    private List<Photo> mPhotoList;

    private Button mSelectButton;

    private ImageButton mBack;

    private SpUtils mSpUtils;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_select;

    }

    @Override
    protected void initView() {
        mSelectPresenter = new SelectGoodsPresenter(this, getContext());
        mSelectEdit = mView.findViewById(R.id.search_edit);
        mSelectRecyclerView = mView.findViewById(R.id.rv_goods_select);
        mBack = mView.findViewById(R.id.back_to_main);
        mSelectButton = mView.findViewById(R.id.select_b);
        mGoodsList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mSelectRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mSelectRecyclerAdapter = new SelectGoodsListAdapter(mGoodsList, mPhotoList);
        mSelectRecyclerView.setAdapter(mSelectRecyclerAdapter);

mSpUtils = new SpUtils(mContext);

    }

    @Override
    protected void initListener() {
        mSelectButton.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.select_b) {
            goodsName = mSelectEdit.getText().toString().trim();
            Log.d(TAG, "Name(SelectFragment):"+goodsName);
            mSelectPresenter.getGoodsList(goodsName);
        } else if (id == R.id.back_to_main) {
            onBack();

        }
    }

    private void onBack() {
        Intent intent = new Intent(getContext(), MainActivity.class);
        startActivity(intent);
    }


    @Override
    public void onResume() {
        super.onResume();
        goodsName = mSpUtils.getString("goods_name");
        mSelectPresenter.getGoodsList(goodsName);
    }

    @Override
    public void showGoodsList(List<Goods> goodsList, List<Photo> photoList) {
        mGoodsList = goodsList;
        mPhotoList = photoList;
        Log.d("", "showGoodsList " + mGoodsList.size());
        mSelectRecyclerAdapter.updateData(mGoodsList , mPhotoList);
    }
}
